package com.fesine.learn.freemarker;

import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 1、自定义口令实现 实现TemplateDirectiveModel接口，暴露成service bean
 * @author: Fesine
 * @createTime:2017/5/8 16:38
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/5/8 16:38
 */
@Service
public class RoleDirectiveModel implements TemplateDirectiveModel {
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels,
                        TemplateDirectiveBody templateDirectiveBody) throws TemplateException,
            IOException {
        System.out.println("=======");
        TemplateScalarModel user = (TemplateScalarModel) map.get("user");
        TemplateScalarModel role = (TemplateScalarModel) map.get("role");
        if ("123456".equals(user.getAsString()) && "admin".equals(role.getAsString())) {
            templateModels[0] = TemplateBooleanModel.TRUE;
        }
        List<String> list = new ArrayList<>();
        list.add("add");
        list.add("update");
        list.add("delete");
        templateModels[1] = new SimpleSequence(list);
        templateDirectiveBody.render(environment.getOut());
    }
}
